package com.sikaryofficial.backend.controller.activity;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sikaryofficial.backend.constant.PrizeLevelEnum;
import com.sikaryofficial.backend.domain.dto.SysDictDataDTO;
import com.sikaryofficial.backend.domain.dto.req.prize.PrizeListReq;
import com.sikaryofficial.backend.domain.dto.req.productactivity.ActivityPrizeRelationReq;
import com.sikaryofficial.backend.domain.dto.resp.prize.PrizeDTO;
import com.sikaryofficial.backend.domain.dto.resp.productactivity.ActivityPrizeRelationDTO;
import com.sikaryofficial.backend.manager.ActivitySettingManager;
import com.sikaryofficial.backend.manager.PrizeManager;
import com.sikaryofficial.common.core.domain.R;
import com.sikaryofficial.common.core.utils.PageUtil;
import com.sikaryofficial.common.core.web.controller.BaseController;
import com.sikaryofficial.common.core.web.domain.PageResult;
import com.sikaryofficial.common.log.annotation.Log;
import com.sikaryofficial.common.log.enums.BusinessType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 活动Controller
 *
 * @author qinjinyuan
 * @date 2023-10-27
 */
@RestController
@RequestMapping("/activity/setting")
@Api(tags = "V3.5-活动奖项设置后台管理")
@Slf4j
public class ActivitySettingController extends BaseController {
    @Autowired
    private ActivitySettingManager activitySettingManager;
    @Autowired
    private PrizeManager prizeManager;

    /**
     * 后台管理-奖项详情
     */
    @GetMapping("/detail/{activityId}/{activityPrizeId}")
    @ApiOperation("奖项详情")
    public R<ActivityPrizeRelationDTO> detail(
            @ApiParam(value = "活动id", required = true)
            @PathVariable(name = "activityId") Long activityId,
            @ApiParam(value = "活动奖项id", required = true)
            @PathVariable(name = "activityPrizeId") Long activityPrizeId) {
        return R.ok(activitySettingManager.detail(activityId, activityPrizeId));
    }

    /**
     * 后台管理-奖项设置列表
     */
    @GetMapping("/{activityId}/list")
    @ApiOperation("奖项设置列表")
    public R<List<ActivityPrizeRelationDTO>> itemList(
            @ApiParam(value = "活动id", required = true)
            @PathVariable(name = "activityId") Long activityId) {
        return R.ok(activitySettingManager.itemList(activityId));
    }

    /**
     * 后台管理-礼品选择列表
     */
    @PostMapping("/{activityId}/prizes")
    @ApiOperation("奖项设置-礼品列表选择")
    public PageResult<PrizeDTO> prizeList(
            @ApiParam(value = "活动id", required = true)
            @PathVariable(name = "activityId") Long activityId,
            @Validated @RequestBody PrizeListReq req) {
        // 查询当前活动下的礼品id
        List<Long> excludePrizeIds = activitySettingManager.getPrizeIdByActivityId(activityId);
        // 查询礼品列表
        IPage<PrizeDTO> page = prizeManager.listPage(req, excludePrizeIds);
        return PageUtil.success(page.getRecords(), page.getTotal());
    }

    /**
     * 后台管理-奖项设置
     */
    @Log(title = "奖项设置", businessType = BusinessType.INSERT)
    @PostMapping("/{activityId}/saveOrUpdate")
    @ApiOperation("奖项设置-批量保存")
    public R<Boolean> saveOrUpdate(
            @ApiParam(value = "活动id", required = true)
            @PathVariable(name = "activityId") Long activityId,
            @Validated @RequestBody ActivityPrizeRelationReq req) {
        activitySettingManager.saveOrUpdate(activityId, req);
        return R.ok(true);
    }

    /**
     * 后台管理-奖项删除
     */
    @Log(title = "奖项删除", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{activityId}/{activityPrizeId}")
    @ApiOperation("奖项删除")
    public R<Boolean> remove(
            @ApiParam(value = "活动id", required = true)
            @PathVariable(name = "activityId") Long activityId,
            @ApiParam(value = "活动奖项id", required = true)
            @PathVariable(name = "activityPrizeId") Long activityPrizeId) {
        activitySettingManager.remove(activityId, activityPrizeId);
        return R.ok(true);
    }

    /**
     * 后台管理-奖项等级
     */
    @GetMapping("/getActivityType")
    @ApiOperation("奖项等级")
    public R<List<SysDictDataDTO>> getPrizeLevel() {
        PrizeLevelEnum[] reportTypeEnums = PrizeLevelEnum.values();
        List<SysDictDataDTO> result = new ArrayList<>();
        for (PrizeLevelEnum reportTypeEnum : reportTypeEnums) {
            SysDictDataDTO sysDictDataDTO = new SysDictDataDTO();
            result.add(sysDictDataDTO);
            sysDictDataDTO.setDictLabel(reportTypeEnum.getDesc());
            sysDictDataDTO.setDictValue(reportTypeEnum.getCode() + "");
            sysDictDataDTO.setDictType("prize_level");
        }
        return R.ok(result);
    }

}
